if( !Function.prototype.bind){
	Function.prototype.bind = function(thisObj){
		var target = this;

		return function(){
			return target.apply(thisObj,arguments);
		};
	};
}

mainCtx = null;
preloader = null;
mainLoop = null;
helpOpen = true;

function makeGameAlready(){
	mainCtx = $('#mainCanvas')[0].getContext('2d');

	preloader = new Preloader();
	preloader.addMulti(pImages);
	
	mainLoop = new GameLoop([
		new Loadingscreen(mainCtx,preloader),
		new Startscreen(mainCtx),
		new ActionScreen(mainCtx)
	]);
	
}

function openHelp(){
	if( helpOpen == true ) return;
	helpOpen = true;
	$('#help').slideDown();
	$('#closehelp').toggle();
	$('#openhelp').toggle();
}

function closeHelp(){
	if( helpOpen == false ) return;
	helpOpen = false;
	$('#help').slideUp();
	$('#closehelp').toggle();
	$('#openhelp').toggle();
}



$(window).load(function(){

	makeGameAlready();

	$('#end a').click(function(event){
		event.preventDefault();
		$('.journal div').remove();
		$('#end').hide();
		$('#talksBackground').hide();
		$('#startscreen>p').hide();
		$('.talksContainer').hide();
		$('.end').hide();
		window.setTimeout(function(){
		 makeGameAlready();
		},500);
		
	});

	$('#closehelp').click(function(event){
		closeHelp();
		event.preventDefault();
	});

	$('#openhelp').click(function(event){
		openHelp();
		event.preventDefault();
	});

});